﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Streambolics.PatLite
{
    /// <summary>
    ///     An object that converts a PhcStatus to a LightpoleStatus
    ///     using the Patlite conventions.
    /// </summary>

    public class PhcToLightpoleConverter : IDisposable
    {
        private PhcStatus _Source;
        private LightpoleStatus _Target;

        public PhcToLightpoleConverter (PhcStatus aSource, LightpoleStatus aTarget)
        {
            _Source = aSource;
            _Target = aTarget;
            _Target.Changed += new EventHandler (_Target_Changed);
        }

        void _Target_Changed (object sender, EventArgs e)
        {
            Convert (_Source, _Target);
        }

        public static Lamp ConvertLamp (Ternary aSteady, Ternary aBlinking)
        {
            switch (aSteady)
            {
                case Ternary.True:
                    return Lamp.On;
                case Ternary.False:
                    switch (aBlinking)
                    {
                        case Ternary.True:
                            return Lamp.Flashing;
                        case Ternary.False:
                            return Lamp.Off;
                    }
                    break;
            }
            return Lamp.Unknown;
        }

        public static Siren ConvertSiren (Ternary aFast, Ternary aSlow)
        {
            switch (aFast)
            {
                case Ternary.True:
                    return Siren.Fast;
                case Ternary.False:
                    switch (aSlow)
                    {
                        case Ternary.True:
                            return Siren.Slow;
                        case Ternary.False:
                            return Siren.Off;
                    }
                    break;
            }
            return Siren.Unknown;
        }
        public static void Convert (PhcStatus aSource, LightpoleStatus aTarget)
        {
            aTarget.Red = ConvertLamp (aSource[0], aSource[5]);
            aTarget.Amber = ConvertLamp (aSource[1], aSource[6]);
            aTarget.Green = ConvertLamp (aSource[2], aSource[7]);
            aTarget.White = Lamp.Off;
            aTarget.Blue = Lamp.Off;
            aTarget.Siren = ConvertSiren (aSource[3], aSource[4]);
        }

        #region IDisposable Members

        public void Dispose ()
        {
            _Target.Changed -= new EventHandler (_Target_Changed);
        }

        #endregion
    }
}
